import {request} from '@/utils/request'
import type { HbtCompanyQuery, HbtCompany, HbtCompanyStatus, HbtCompanyUpdate, HbtCompanyCreate } from '@/types/accounting/financial/company'
import type { HbtPagedResult, HbtSelectOption } from '@/types/common'

/**
 * 获取公司分页列表
 */
export function getCompanyList(query: HbtCompanyQuery) {
  return request<HbtPagedResult<HbtCompany>>({
    url: '/api/HbtCompany/list',
    method: 'get',
    params: query
  })
}

/**
 * 获取公司详情
 */
export function getByIdAsync(companyId: string) {
  return request<HbtCompany>({
    url: `/api/HbtCompany/${companyId}`,
    method: 'get'
  })
}

/**
 * 获取公司选项列表
 */
export function getCompanyOptions() {
  return request<HbtSelectOption[]>({
    url: '/api/HbtCompany/options',
    method: 'get'
  })
}

/**
 * 创建公司
 */
export function createCompany(data: HbtCompanyCreate) {
  return request<number>({
    url: '/api/HbtCompany',
    method: 'post',
    data
  })
}

/**
 * 更新公司
 */
export function updateCompany(data: HbtCompanyUpdate) {
  return request<boolean>({
    url: '/api/HbtCompany',
    method: 'put',
    data
  })
}

/**
 * 删除公司
 */
export function deleteCompany(companyId: string) {
  return request<boolean>({
    url: `/api/HbtCompany/${companyId}`,
    method: 'delete'
  })
}

/**
 * 批量删除公司
 */
export function batchDeleteCompany(companyIds: string[]) {
  return request<boolean>({
    url: '/api/HbtCompany/batch',
    method: 'delete',
    data: companyIds
  })
}

/**
 * 更新公司状态
 */
export function updateCompanyStatus(data: HbtCompanyStatus) {
  return request<boolean>({
    url: `/api/HbtCompany/${data.companyId}/status`,
    method: 'put',
    params: {
      status: data.status
    }
  })
}

/**
 * 导入公司数据
 */
export function importCompany(file: File, sheetName: string = 'Company') {
  const formData = new FormData()
  formData.append('file', file)
  return request<{ success: number; fail: number }>({
    url: `/api/HbtCompany/import?sheetName=${sheetName}`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 导出公司数据
 */
export function exportCompany(query: HbtCompanyQuery, sheetName: string = 'Company') {
  return request<Blob>({
    url: `/api/HbtCompany/export?sheetName=${sheetName}`,
    method: 'get',
    params: query,
    responseType: 'blob'
  })
}

/**
 * 获取公司导入模板
 */
export function getTemplate(sheetName: string = 'Company') {
  return request<Blob>({
    url: `/api/HbtCompany/template?sheetName=${sheetName}`,
    method: 'get',
    responseType: 'blob'
  })
}